<script src="/static/js/md5.js"></script>


<div class="row">
    <div class="col-xs-12">
        <!-- PAGE CONTENT BEGINS -->
        <form class="form-horizontal" method="post" role="form">
            <!-- #section:elements.form -->
            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">项目名: </label>
                <div class="col-sm-6">
                    <input name="vars[name]" class="col-xs-8 col-sm-6" type="text" value="{{.data.Name}}" required>
                </div>
                <div class="col-sm-4">
                    网站名称
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">官方网站: </label>
                <div class="col-sm-6">
                    <select class="col-sm-3 select" name="vars[is_official]">
                        <option value="0" {{if eq "0" .data.IsOfficial}}selected="selected"{{else}}{{.data.IsOfficial}}selected="selected"{{end}}>否</option>                        
                        <option value="1" {{if eq "1" .data.IsOfficial}}selected="selected"{{else}}{{.data.IsOfficial}}{{end}}>是</option>
                    </select>
                </div>
                <div class="col-sm-4">
                    是否是官方网站
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">网站地址: </label>
                <div class="col-sm-6">
                    <input name="vars[page_index]" class="col-xs-8 col-sm-6" type="text" value="{{.data.PageIndex}}" required>
                </div>
                <div class="col-sm-4">
                    网站地址
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">页面编码:</label>
                <div class="col-sm-6" >
                    <input  class="col-sm-6" type="text" name="vars[page_charset]" value='{{if eq "" .data.PageCharset}}utf8{{else}}{{.data.PageCharset}}{{end}}' required>
                </div>
                <div class="col-sm-4">
                    网站编码(仅支持utf8和gbk编码)
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">首页规则: </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="5" name="vars[page_index_rule]" required>{{.data.PageIndexRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_page_index_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    首页规则类型多,要支持多个规则, 用户"|"或回车分割 <br /> 
                    匹配小说地址(唯一表示ID)和名字(使用小说目录模板)
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">小说介绍页规则: </label>
                <div class="col-sm-5">
                    <input type="text" class="col-sm-12" name="vars[path_rule]" value="{{.data.PathRule}}" required>
                </div>
                <div class="col-sm-1">
                    <div id="reg_path_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    首页上匹配章节首页规则,<br/> 例如:https://xx.com/(\d*).(html)
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">小说目录模板: </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" type="text" name="vars[path_tpl]" value="{{.data.PathTpl}}" required/>
                </div>

                <div class="col-sm-4">
                    小说目录模板<br/> 例如:https://xx.com/{$ID}.html
                </div>
            </div>

            <hr />

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">目录页样本页: </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" type="text" name="vars[path_page_exp]" value="{{.data.PathPageExp}}">
                </div>

                <div class="col-sm-4">
                    目录页样本页
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">小说名规则: </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[name_rule]">{{.data.NameRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_name_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配小说名规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">作者规则: </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[author_rule]">{{.data.AuthorRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_author_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配作者规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">简介(描述)规则: </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[desc_rule]">{{.data.DescRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_desc_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    简介(描述)规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">小说分类规则: </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12"rows="3" name="vars[category_rule]">{{.data.CategoryRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_category_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配分类规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    小说状态规则: 
                </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[status_rule]">{{.data.StatusRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_status_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配状态规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    小说完本标志: 
                </label>
                <div class="col-sm-1">
                    <input  class="col-sm-12" type="text" name="vars[status_end_mark]" value='{{if eq "" .data.StatusEndMark}}完本{{else}}{{.data.StatusEndMark}}{{end}}'>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                     章节首页规则: 
                </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[chapter_path_rule]">{{.data.ChapterPathRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_chapter_path_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配章节首页规则(如果为空,则任务信息页和列表在一起)
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 章节首页样本页: </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" type="text" name="vars[chapter_path_exp]" value="{{.data.ChapterPathExp}}">
                </div>

                <div class="col-sm-4">
                    章节首页样本页
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    章节列表规则: 
                </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[chapter_list_rule]">{{.data.ChapterListRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_chapter_list_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    匹配章节列表规则
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 章节内容样本页: </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" class="col-xs-8 col-sm-4" type="text" name="vars[content_exp]" value="{{.data.ContentExp}}">
                </div>

                <div class="col-sm-4">
                     章节内容样本页
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    章节内容规则: 
                </label>
                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[content_rule]">{{.data.ContentRule}}</textarea>
                </div>
                <div class="col-sm-1">
                    <div id="reg_content_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>

                <div class="col-sm-4">
                    章节内容规则
                </div>
            </div>
            <hr />
            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    搜索页样本页: 
                </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" type="text" name="vars[soso_exp]" value="{{.data.SosoExp}}">
                </div>
               
                <div class="col-sm-3">
                    搜索页样本页,<br />
                    https://xx/?k={$KEYWORD}<br/>
                    {$KEYWORD}搜索关键字. <br/>
                    匹配小说地址(唯一表示ID)和名字(使用小说目录模板)
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    参数(关键字): 
                </label>
                <div class="col-sm-2">
                    <input class="col-sm-12" type="text" name="vars[soso_exp_keyword]" value="凡人修仙传">
                </div>

                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                    参数(分页): 
                </label>
                <div class="col-sm-1">
                    <input  class="col-sm-12" type="text" name="vars[soso_page_args]" value='{{if eq "" .data.SosoPageArgs}}page{{else}}{{.data.SosoPageArgs}}{{end}}'>
                </div>

                <div class="col-sm-1">
                    <input  class="col-sm-12" type="text" name="vars[soso_page_val]" value='1'>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    小说页匹配规则: 
                </label>

                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[soso_rule]">{{.data.SosoRule}}</textarea>
                </div>

                <div class="col-sm-1">
                    <div id="reg_soso_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>
               
                <div class="col-sm-4">
                    匹配小说地址(唯一表示ID)和名字(使用小说目录模板)
                </div>
            </div>

            <hr/>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    全站爬取页样本页: 
                </label>
                <div class="col-sm-6">
                    <input class="col-sm-12" type="text" name="vars[spider_exp]" value="{{.data.SpiderExp}}">
                </div>
               
                <div class="col-sm-4">
                    http://xx/{$RANGE}.html|
                    {$RANGE}是一个数据范围,比如:(1,2).
                </div>
            </div>


            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    参数(range): 
                </label>
                <div class="col-sm-1">
                    <input class="col-sm-12" type="text" name="vars[spider_range]" value='{{if eq "" .data.SpiderRange}}1,100{{else}}{{.data.SpiderRange}}{{end}}'>
                </div>

                <label class="col-sm-2 control-label no-padding-right" for="form-field-2">
                    参数(开始页): 
                </label>
                <div class="col-sm-1">
                    <input  class="col-sm-12" type="text" name="vars[spider_range_start]" value='1'>
                </div>

                <label title="仅支持utf8和gbk编码" class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    参数(公用上面编码)
                    如果range中开始和结束相等,则认为是单页爬取
                </label>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
                    全站爬取匹配规则: 
                </label>

                <div class="col-sm-5">
                    <textarea class="col-sm-12" rows="3" name="vars[spider_rule]">{{.data.SpiderRule}}</textarea>
                </div>

                <div class="col-sm-1">
                    <div id="reg_spider_rule_test" class="btn btn-sm btn-primary">匹配</div>
                </div>
               
                <div class="col-sm-4">
                    匹配小说地址(唯一表示ID)和名字(使用小说目录模板)
                </div>
            </div>

            <!-- <div class="space-4"></div> -->
            <input name="id" type="hidden" value="{{.id}}">
            <div class="clearfix form-actions">
                <div class="col-md-offset-3 col-md-9">
                    <button class="btn btn-info" type="submit">
                        <i class="ace-icon fa fa-check bigger-110"></i>提交
                    </button>
                    &nbsp; &nbsp; &nbsp;
                    <button class="btn" type="reset">
                        <i class="ace-icon fa fa-undo bigger-110"></i>重置
                    </button>
                    &nbsp; &nbsp; &nbsp;
                    <button class="btn" type="all_val">
                        <i class="ace-icon fa fa-check bigger-110"></i>全部匹配
                    </button>
                </div>
            </div>

        </form>
    </div><!-- /.col -->
</div>
<script src="/static/js/gbk/gbk.js"></script>

<style type="text/css">
.noticeFontSize{
    font-size: 5px;
}
</style>

<script>

function HtmlEncode(input) { 
    var converter = document.createElement("DIV"); 
    converter.innerText = input; 
    var output = converter.innerHTML; 
    converter = null; 
    return output; 
}

function sleep(numberMillis) { 
    var now = new Date(); 
    var exitTime = now.getTime() + numberMillis; 
    while (true) { 
        now = new Date(); 
        if (now.getTime() > exitTime) 
            return; 
    } 
}

jQuery(function($) {
//start

function vefiy_rule(url, rule, charset, model, path_tpl){
    $.post("/{{adminPath}}/{{$.curController}}/verify", { url: url, rule: rule ,charset: charset, model:model, path_tpl:path_tpl}, function(data){
        var content = "";
        if (data['code'] < 0){
            content = data['msg'];
        } else {
            
            if (data['data'][0]){

                if(data['data'][0].length>1){
                    //页面上只显示一条数据
                    var t = "";
                    for (i in data['data'][0][0]) {
                        t += data['data'][0][0][i] + ",\n";
                        console.log(t);
                    }
                    content += t;
                    content += "---------------------------------\n";
                    content += "页面上只显示一条数据,更多看console.log";
                    console.log(url,rule,data["data"][0]);
                } else {
                    for (j in data['data']) {
                        var t = "";
                        for (i in data['data'][j]) {
                            t += data['data'][j][i] + "\n";
                        }
                        content += t;
                    } 
                }
            } else {
                content = "没有匹配到!";
            }
        }
        content = "规则:"+ rule + "\n返回匹配数据:\n" + content;
        content = HtmlEncode(content);
        $.gritter.add({
            title: "规则匹配",
            text: content,
            class_name: 'gritter-info gritter-light noticeFontSize'
        });
    }, "json");
}


//验证目录页面
$('#reg_page_index_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[page_index]']").val();
    var rule = $(".form-group :input[name='vars[page_index_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    var path_tpl = $(".form-group :input[name='vars[path_tpl]']").val();
    var model = 1;

    rule = rule.trim();
    rule = rule.replace("\n", "|");
    rule = rule.replace("\r\n", "|");

    rule_list = rule.split("|");
    for(i in rule_list) {
        if (i>0){
            var tmp_rule = rule_list[i];
            setTimeout(function(){
                vefiy_rule(url,tmp_rule,charset,model, path_tpl);
            },i*1000); 
        } else {
            vefiy_rule(url,rule_list[i],charset,model, path_tpl);
        }
    }
});


//验证目录页面
$('#reg_path_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[page_index]']").val();
    var rule = $(".form-group :input[name='vars[path_rule]']").val();
    vefiy_rule(url,rule);
});

//验证小说名
$('#reg_name_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[name_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证小说简介
$('#reg_desc_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[desc_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证小说作者
$('#reg_author_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[author_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证小说分类
$('#reg_category_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[category_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证小说状态
$('#reg_status_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[status_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证章节首页列表
$('#reg_chapter_path_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[path_page_exp]']").val();
    var rule = $(".form-group :input[name='vars[chapter_path_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证章节列表
$('#reg_chapter_list_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[chapter_path_exp]']").val();
    var rule = $(".form-group :input[name='vars[chapter_list_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证章节内容
$('#reg_content_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[content_exp]']").val();
    var rule = $(".form-group :input[name='vars[content_rule]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    vefiy_rule(url,rule,charset);
});

//验证soso内容
$('#reg_soso_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[soso_exp]']").val();
    var keyword = $(".form-group :input[name='vars[soso_exp_keyword]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    var page = $(".form-group :input[name='vars[soso_page_args]']").val();
    var page_val = $(".form-group :input[name='vars[soso_page_val]']").val();
    if ("gbk" == charset){
        keyword = $URL.encode(keyword);
    } else if ("utf8" == charset) {
        keyword = encodeURIComponent(keyword);
    }

    url = url.replace(/{\$KEYWORD}/,keyword);
    if (url.indexOf('?') !== -1){
        url += '&'+page+'='+page_val;
    } else {
        url += '?'+page+'='+page_val;
    }

    var path_tpl = $(".form-group :input[name='vars[path_tpl]']").val();
    // var model = $(".form-group :input[name='vars[soso_model]']").val();
    var model = 1;
    var rule = $(".form-group :input[name='vars[soso_rule]']").val();
    console.log(url, rule, charset, model, path_tpl);

    if (model == "0"){
        vefiy_rule(url, rule, charset, model, path_tpl);
    } else {
        vefiy_rule(url, rule, charset, model, path_tpl);
    }
});


//爬取测试
$('#reg_spider_rule_test').click(function(){
    var url = $(".form-group :input[name='vars[spider_exp]']").val();
    var page_start = $(".form-group :input[name='vars[spider_range_start]']").val();
    var charset = $(".form-group :input[name='vars[page_charset]']").val();
    var path_tpl = $(".form-group :input[name='vars[path_tpl]']").val();
    var rule = $(".form-group :input[name='vars[spider_rule]']").val();
    var model = 1;

    url = url.replace(/{\$RANGE}/,page_start);

    if (url == ""){
        alert("爬取页样本页,没有填写!");
    }

    console.log(url, page_start);
    vefiy_rule(url, rule, charset,model, path_tpl);
});

//end
})
</script>